# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
from ..base_parser import BasePageParser
from fire_control_spider.utils import ContentProcessor
from .common import update_media_urls

class CfastManualParser(BasePageParser):
    """新闻列表页解析器"""

    def __init__(self, spider):
        super().__init__(spider)
        self.site_name = 'nist'
    
    def can_handle(self, response):
        return 'manuals.html' in response.url
    
    def parse(self, response):
        # 创建基础item
        item = self.create_webpage_item(response)
        
        # 使用BeautifulSoup提取标题
        soup = BeautifulSoup(response.text, 'html.parser')

        menu = "FDS-SMV" if 'fds-smv' in response.url else 'CFAST'
        
        # 提取菜单信息
        item['category'] = ['Home', 'Resources', menu, 'Manuals']
        
        title = soup.select_one('h1').get_text(strip=True)
        item['title'] = title
        item['publish_time'] = ''
        
        # 提取正文内容并处理图文交错
        main_content = soup.select_one('#main_content')

        if main_content:
            main_content = str(main_content)
    
        
        if main_content:
            main_body, media_urls = ContentProcessor.process_content_with_media(
                main_content, response.url, self.site_name
            )
            item['main_body'] = main_body

            # 更新媒体URLs
            update_media_urls(item, media_urls)


        # 添加新闻特有信息
        item['remark'] = {}
        
        yield item